package com.sdcqjy.property.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.presenter.contract.RegContract;

import java.util.HashMap;
import java.util.Map;

import static com.sdcqjy.mylibrary.http.HttpClient.HttpType.JSON;
import static com.sdcqjy.property.presenter.contract.a.RegisterUser;
import static com.sdcqjy.property.presenter.contract.a.SmsCode;

/**
 * Created by 磁磁帅 on 2018/4/29.
 * 功能：
 */

public class RegPresenter extends BaseContract.Presenter<RegContract.View> {
    public RegPresenter(RegContract.View baseView) {
        super(baseView);
    }

    public void sendMsm(String tel) {
        new HttpClient()
                .addParam("telePhone", tel)
                .setHttpType(JSON).httpRequest(SmsCode, new BaseRequestBack<BaseRet>() {
            public BaseRet jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, BaseRet.class);
            }

            public void onResponseSucc(BaseRet ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.sendMsmRet(ret);
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void reg(String tel, String name, String pwd, String code, String email) {
        Map<String, Object> param = new HashMap<>();
        param.put("smsCode", code);
        param.put("userName", name);
        param.put("passWord", pwd);
        if (!TextUtils.isEmpty(email)) param.put("email", email);
        param.put("telePhone", tel);
        new HttpClient()
                .setParam(param)
                .setHttpType(JSON).httpRequest(RegisterUser, new BaseRequestBack<BaseRet>() {
            public BaseRet jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, BaseRet.class);
            }

            public void onResponseSucc(BaseRet ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.regSucc();
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }
}
